package com.shire.shire_interface.service.clientversion;

import com.shire.shire_interface.constants.Constants;
import com.shire.shire_interface.dao.clientversion.ClientVersionDao;
import com.shire.shire_interface.model.clientversion.ClientVersion;
import com.shire.shire_interface.model.clientversion.ClientVersionExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClientVersionService {
    @Resource
    private ClientVersionDao clientVersionDao;

    public List<ClientVersion> getByExample(ClientVersionExample example){
        return clientVersionDao.selectByExample(example);
    }

    public ClientVersion getValidVersion(){
        ClientVersionExample example = new ClientVersionExample();
        example.createCriteria().andStatusEqualTo(Constants.CommonStatus.ENABLE.getCode());
        List<ClientVersion> list = clientVersionDao.selectByExample(example);
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    public int insertClientVersion(ClientVersion clientVersion){
        ClientVersionExample example = new ClientVersionExample();
        example.createCriteria().andStatusEqualTo(Constants.CommonStatus.ENABLE.getCode());
        ClientVersion oldVersion = new ClientVersion();
        oldVersion.setStatus(Constants.CommonStatus.DISABLE.getCode());
        clientVersionDao.updateByExampleSelective(oldVersion,example);
        return clientVersionDao.insert(clientVersion);
    }
}
